﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.Runtime.InteropServices;
using System.Threading;
using MoteurAudio;
using Guitar;
using Guitar.Modules.Manche;
using Modele;


namespace Module
{

    /// <summary>
    /// Logique d'interaction pour Accordeur.xaml
    /// </summary>
    public partial class Manche : Grid
    {

        /* Attributs du manche */
        public enum langageNotation { Fr, En }; //langue
        public Note[] accordage;

        // collection de notes sur le manche
        List<MiniatureNote> notesManche = new List<MiniatureNote>();


        // Tout ce qui est à nettoyer
        private Note[] noteCorde;
        private MiniatureNote[,] tmp;
        private string[] nom;



        public Manche()
        {
            InitializeComponent();
            noteCorde = new Note[23];
            tmp = new MiniatureNote[6, 23];
            accordage = null;
            try
            {
                accordage = new Note[App.Partitions[App.PartitionEnCours].pistes[App.PartitionEnCours].tablature.accordage.Count];
                App.Partitions[App.PartitionEnCours].pistes[App.PartitionEnCours].tablature.accordage.CopyTo(accordage, 0);
                DeclarerNotes();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

        }


        public void DeclarerNotes()
        {


            // déclaration statiques des notes sur le manche ( 19 x6 notes)

            for (int cpt = 5; cpt >= 0; cpt--)
            {

                noteCorde[0] = (Note)accordage[cpt].Copy();


                for (int j = 0; j < 23; j++)
                {
                    if (j < 22)
                    {
                        noteCorde[j + 1] = (Note)noteCorde[j].Copy();
                        noteCorde[j + 1]++;

                    }

                    nom = noteCorde[j].ToString(App.option.LangueNotationMusicale);
                    tmp[cpt, j] = new MiniatureNote(nom[0] + nom[1]);
                    tmp[cpt, j].Tool.Content = "Octave : " + noteCorde[j].octave +"\nFreq : "+noteCorde[j].freqHz+"\nCase : "+j;
                    tmp[cpt, j].note = noteCorde[j];
                    tmp[cpt, j].note.tablatureInfos.numCorde = cpt;
                    tmp[cpt, j].note.tablatureInfos.numCase = j;

                    noteCorde[j] = null;


                    if (cpt == 5) { corde0.Children.Add(tmp[cpt, j]); }
                    if (cpt == 4) { corde1.Children.Add(tmp[cpt, j]); }
                    if (cpt == 3) { corde2.Children.Add(tmp[cpt, j]); }
                    if (cpt == 2) { corde3.Children.Add(tmp[cpt, j]); }
                    if (cpt == 1) { corde4.Children.Add(tmp[cpt, j]); }
                    if (cpt == 0) { corde5.Children.Add(tmp[cpt, j]); }

                    nom = null;
                }

                //Déclarations statiques des positions des notes sur le manche

                tmp[cpt, 0].Margin = new Thickness(0, 0, 0, 0);
                tmp[cpt, 1].Margin = new Thickness(30, 0, 0, 0);
                tmp[cpt, 2].Margin = new Thickness(75, 0, 0, 0);
                tmp[cpt, 3].Margin = new Thickness(120, 0, 0, 0);
                tmp[cpt, 4].Margin = new Thickness(170, 0, 0, 0);
                tmp[cpt, 5].Margin = new Thickness(215, 0, 0, 0);
                tmp[cpt, 6].Margin = new Thickness(260, 0, 0, 0);
                tmp[cpt, 7].Margin = new Thickness(303, 0, 0, 0);
                tmp[cpt, 8].Margin = new Thickness(345, 0, 0, 0);
                tmp[cpt, 9].Margin = new Thickness(385, 0, 0, 0);
                tmp[cpt, 10].Margin = new Thickness(420, 0, 0, 0);
                tmp[cpt, 11].Margin = new Thickness(450, 0, 0, 0);
                tmp[cpt, 12].Margin = new Thickness(478, 0, 0, 0);
                tmp[cpt, 13].Margin = new Thickness(503, 0, 0, 0);
                tmp[cpt, 14].Margin = new Thickness(526, 0, 0, 0);
                tmp[cpt, 15].Margin = new Thickness(548, 0, 0, 0);
                tmp[cpt, 16].Margin = new Thickness(569, 0, 0, 0);
                tmp[cpt, 17].Margin = new Thickness(590, 0, 0, 0);
                tmp[cpt, 18].Margin = new Thickness(613, 0, 0, 0);
                tmp[cpt, 19].Margin = new Thickness(632, 0, 0, 0);
                tmp[cpt, 20].Margin = new Thickness(655, 0, 0, 0);
                tmp[cpt, 21].Margin = new Thickness(677, 0, 0, 0);
                tmp[cpt, 22].Margin = new Thickness(698, 0, 0, 0);

            }
            accordage = null;
        }





        public void UpdateNote()
        {

            // déclaration statiques des notes sur le manche ( 19 x6 notes)
            accordage = new Note[App.Partitions[App.PartitionEnCours].pistes[App.PartitionEnCours].tablature.accordage.Count];
            App.Partitions[App.PartitionEnCours].pistes[App.PartitionEnCours].tablature.accordage.CopyTo(accordage, 0);
            for (int cpt = 5; cpt >= 0; cpt--)
            {

                noteCorde[0] = (Note)accordage[cpt].Clone();

                for (int j = 0; j < 23; j++)
                {
                    if (j < 22)
                    {
                        noteCorde[j + 1] = (Note)noteCorde[j].Clone();
                        noteCorde[j + 1]++;

                    }
                 
                    nom = noteCorde[j].ToString(App.option.LangueNotationMusicale);
                    tmp[cpt, j].note.freqHz = noteCorde[j].freqHz;
                    tmp[cpt, j].note.octave = noteCorde[j].octave;
                    tmp[cpt, j].Content = nom[0] + nom[1];
                    tmp[cpt, j].Tool.Content = "Octave : " + noteCorde[j].octave + "\nFreq : " + noteCorde[j].freqHz;
                    noteCorde[j] = null;

                    nom = null;
                }

                accordage[cpt] = null;

            }

            accordage = null;

        }
       


    }
}
